package com.gdwstudy.sellTicket;

import com.sun.xml.internal.fastinfoset.util.ValueArrayResourceException;

/**
 * @author 关道伟
 * @version 1.0
 * 使用线程模拟三个窗口进行同时售票
 */
public class SellTicket {
    public static void main(String[] args) {
        SellTicket01 sellTicket01 = new SellTicket01();
        SellTicket01 sellTicket02 = new SellTicket01();
        SellTicket01 sellTicket03 = new SellTicket01();
        //这里可能会出现票数超卖的问题
        sellTicket01.start();
        sellTicket02.start();
        sellTicket03.start();

    }
}

//Thread
class SellTicket01 extends Thread{

    private static int ticketnum = 20 ;//多个线程共享资源
    @Override
    public void run() {

        while(ticketnum>0){
            //有票 售票时间1秒
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(ticketnum<=0){
                System.out.println("售票结束...");
                return;
            }
            System.out.println("窗口"+Thread.currentThread().getName()+" 售出一张票"
                    +"剩余票数"+(--ticketnum));
        }

    }
}
